package com.example.examplecarousel.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.examplecarousel.MainActivity;
import com.example.examplecarousel.R;

import java.util.ArrayList;

/**
 * @ClassName MyAdapter
 * @Description TODO
 * @Author ${孙伟豪}
 * @Date 2020/12/25 9:32
 * @Version 1.0
 */
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
    private final Context context;
    private final ArrayList<Integer> arrayList;

    public MyAdapter(Context context, ArrayList<Integer> arrayList) {
        this.context=context;
        this.arrayList=arrayList;
    }

    @NonNull
    @Override
    public MyAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(context).inflate(R.layout.item_main_test,parent,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyAdapter.ViewHolder holder, int position) {
        if (arrayList!=null) {
            holder.mIvBackground.setImageResource(arrayList.get(position));
        }
    }

    @Override
    public int getItemCount() {
        return arrayList==null?0:arrayList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private ImageView mIvBackground;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            mIvBackground = itemView.findViewById(R.id.iv_background);
        }
    }

}
